Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MQTT improvements and fixes #2

Closed
wants to merge 17 commits into from
Closed

MQTT improvements and fixes #2

wants to merge 17 commits into from

Conversation

rechrtb
Copy link
Owner

@rechrtb rechrtb commented Sep 8, 2023

Follow up to Duet3D#589.

  • Change subscription max QOS parameter from Q to O in M586.4
  • Removed single default topic configuration in M586.4.
  • Topic to publish to must be specified in M118 via parameter T . QOS, retain and duplicate flags can be optionally specified via Q, R and D in M118
  • Support LWT (last will and testament) QOS and retain flag via parameter Q and R in M586.4.
  • MQTT demonstration script and documentation moved to Duet3D/MQTT-WPA2-Enterprise-Demo.

Closes Duet3D#880.

@rechrtb rechrtb force-pushed the mqtt_fixes branch 6 times, most recently from e10c385 to 8e3777c Compare September 16, 2023 02:51
@rechrtb
Copy link
Owner Author

rechrtb commented Sep 16, 2023

@PaulG4H, if you want to test early :) I'm testing with the setup from https://github.com/rechrtb/Duet-Demo/tree/master/mqtt, and seems to work ok when following the README, but I'm still double checking the code if I miss some potential bugs.

@rechrtb rechrtb changed the title [WIP] MQTT fixes [WIP] MQTT improvements and fixes Sep 16, 2023
@rechrtb rechrtb force-pushed the mqtt_fixes branch 6 times, most recently from 6b3f92e to 0900ffe Compare September 22, 2023 00:57
@rechrtb rechrtb changed the title [WIP] MQTT improvements and fixes MQTT improvements and fixes Sep 22, 2023

uint32_t messageTimer; // General purpose variable for keeping track of queued messages timeout
Mutex publishMutex;

MqttClient *next;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under what conditions can we have more than one MqttClient?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The case where more than one MqttClient instance is needed is for when there are multiple brokers - either on the same interface or on different interfaces.

Per our email conversation, only one broker on one interface needs to be supported for now. So I changed MqttClient to be a singleton. This is remnant of the old code I forgot to delete.

#define MQTT_PAL_MUTEX_LOCK(x)
#define MQTT_PAL_MUTEX_INIT(x)
#define MQTT_PAL_MUTEX_UNLOCK(x)
typedef struct
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please confirm that we need to use a mutex, i.e. there can be more than one task accessing those parts of the system that the mutex guards. Also, are we certain that no deadlock can occur?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mutex is for guarding resources between (1) the main task when it calls mqtt_publish - when buffering the message for sending and (2) the network task when it calls mqtt_sync - when moving data from mqtt buffer to socket buffer.

As to whether deadlocks can occur, one of the condition for it does not seem to be satisfied - circular wait. mqtt_sync and mqtt_publish only share 1 common mutex they are both attempting to lock; and mqtt_publish itself only tries to attempt to lock 1 mutex, breaking the possibility of a circular wait chain from forming.

@rechrtb rechrtb force-pushed the mqtt_fixes branch 2 times, most recently from a9c67a5 to 3478a74 Compare September 25, 2023 06:18
@dc42
Copy link

dc42 commented Sep 25, 2023 via email

@rechrtb rechrtb force-pushed the mqtt_fixes branch 3 times, most recently from d4893ad to ce4a6a8 Compare September 26, 2023 12:22
@PaulG4H
Copy link

PaulG4H commented Sep 29, 2023

@rechrtb sorry for the late reply,

Many thank's for your work to bring mqtt native to RRF!

I testet the current 3.5.0-rc.1+102 Version today, it works better but the Topic in M118 gets corrupted --> when I run:
M118 P6 S"idle" T"devices/rrf-k40/status" R1

The broker receives only:
image

I have no clue why the topic is corrupted to only >k40<, instead of the full path >devices/rrf-k40/status<

After further testing, the topic is ignored totaly, instead the machine Name is used.

It seams that my ESP32 Firmware is not working correctly?

@rechrtb rechrtb closed this Oct 1, 2023
@rechrtb rechrtb reopened this Oct 1, 2023
@rechrtb
Copy link
Owner Author

rechrtb commented Oct 1, 2023

Hi @PaulG4H, no worries.

I tested sending M118 P6 S"idle" T"devices/rrf-k40/status" R1 with the setup from the demo, and seems to work ok. No topic corruption or anything like that.

I will have to ask where did you get the binary for the RRF version you tested? You might be testing one that don't have these changes yet, or at least the latest changes in this pull request. Can you try doing M118 without the T parameter? If it doesn't complain of a missing parameter, it means you're on an older version without these changes.

I'm not sure if you are able to build RRF with the changes in this pull request. If not, I can send you a build - though don't know the board you're using. Furthermore, you might be apprehensive of trusting some binary from some stranger online (rightfully so!).

@dc42 can I share with @PaulG4H a built binary with this PR using the Duet3D Dropbox?

@PaulG4H
Copy link

PaulG4H commented Oct 1, 2023

@rechrtb I already post this issue in the RRF discord --> I use a SKR2 board with the binary created by GloomyAndy and his Version is not the latest.

So I will wait for that.

I also see in the official documentation the LWT config, this is so great either!

When your changes are applied to the RRF binaries this opens a whole now world!

Smarthome Integration without pooling, no matter if you use Home Assistant, IP Symcon, IO Broker, OpenHab or any other which is able to receive MQTT topics!

Or switch a external relay from GCode (Tasmota or Shelly Device) directly over WiFi for example the Dust Collector for the CNC or the Chiller for the Laser...

Many thanks for your time and effort!

@rechrtb
Copy link
Owner Author

rechrtb commented Oct 2, 2023 via email

@dc42
Copy link

dc42 commented Oct 10, 2023

@rechrtb please make that change and then merge this PR into your 3.5-dev branch; then let me know and I will merge your 3.5-dev branch into the master.

@rechrtb
Copy link
Owner Author

rechrtb commented Oct 25, 2023

Closing due to mistake in target branch. See Duet3D#923 instead.

@rechrtb rechrtb closed this Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants